package com.polimi.meteocal.security;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultScheduleEvent;
import org.primefaces.model.LazyScheduleModel;
import org.primefaces.model.ScheduleEvent;
import org.primefaces.model.ScheduleModel;

import com.polimi.meteocal.business.control.CalendarManager;
import com.polimi.meteocal.business.control.EventManager;
import com.polimi.meteocal.business.control.UserManager;
import com.polimi.meteocal.business.entity.Event;

import java.io.Serializable;

@ManagedBean
@RequestScoped
public class ScheduleBean implements Serializable {
/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/*
 * ScheduleBean is linked to calendarPage and loads events on PrimeFaces component. 
 * It is also involved in the response to events generated on the schedule component-MISSING   
 */
	@EJB
	CalendarManager cm;
	@EJB
	EventManager em;
	@EJB
	UserManager um;
	
	private ScheduleModel lazyModel;
	
	
	
	@PostConstruct
	public void init(){	
		System.out.println("SCHEDULE BEAN INIT");
		
		//Check if there is a calendar to search (request by user)
		FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        ServletContext sContext = request.getSession().getServletContext();
        List<Event> userEvents=  (List<Event>) sContext.getAttribute("userEvents");
        
        
        //If there is a calendar to search, load it
        if(userEvents!=null){
        	System.out.println("Calendar Request");
        	loadCal(userEvents);
        }
        else{
        //Else, load logged user's calendar	
        	System.out.println("User's Calendar");
        	List<Event> events= new ArrayList<Event>();
        	loadCal(events);
        }
        
	}
	
	public void loadCal(List<Event> events){
	//This metod loads the loazy model on the schedule component.
	//There are two possible execution paths:
	//	1.load logged user's calendar
	//	2.load a public calendar
       if(events.isEmpty()){
    	   System.out.println("Loading user's calendar");
    	   this.lazyModel=null;
			this.setLazyModel(new LazyScheduleModel(){
		        /**
				 * 
				 */
				private static final long serialVersionUID = 1L;
		        
				List<Event> userEvents= cm.getUserEvents();
		        Iterator<Event> it= userEvents.iterator();
				
				@Override
				public void loadEvents(Date start, Date end){
					while(it.hasNext()){
						Event event= it.next();
						
						Date d= event.getDatetimebegin();
						
						
						DefaultScheduleEvent evToLoad= new DefaultScheduleEvent(event.getName(), d, d);
						evToLoad.setData(event);
						lazyModel.addEvent(evToLoad);
					}
				}
			});
        }
       
       else{
    	   System.out.println("Loading request calendar");
    	   this.lazyModel=null;
    	   this.setLazyModel(new LazyScheduleModel(){
    			
		        /**
				 * 
				 */
				private static final long serialVersionUID = 1L;
		        
				List<Event> userEvents= events;
		        Iterator<Event> it= userEvents.iterator();
				
				@Override
				public void loadEvents(Date start, Date end){
					while(it.hasNext()){
						Event event= it.next();
						
						Date d= event.getDatetimebegin();
						
						
						
						DefaultScheduleEvent evToLoad= new DefaultScheduleEvent(event.getName(), d, d);
						evToLoad.setStyleClass("myclass");
						lazyModel.addEvent(evToLoad);
					}
				}
			});
    	   
       }
	}
	
	public String reload(){
		//When the new calendar is completely loaded, it is set to null for the next research
 	   	FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        ServletContext sContext = request.getSession().getServletContext();
        sContext.setAttribute("userEvents", null);
		return "calendarPage";
	}
	
	public void onEventSelect(SelectEvent selectEvent) throws IOException {
		ScheduleEvent event = (ScheduleEvent) selectEvent.getObject(); 
		Event ev= (Event) event.getData();
	}

	public ScheduleModel getLazyModel() {
		return lazyModel;
	}

	public void setLazyModel(ScheduleModel lazyModel) {
		this.lazyModel = lazyModel;
	}
}
